package com.taotao.Controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.pojo.TbContentCategory;
import com.taotao.service.ContentCategoryService;

@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {
	@Autowired
	private ContentCategoryService contentCategoryService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	@ResponseBody
	public List<Map> getListByParentId(@RequestParam(defaultValue = "0")Long id) {
		List result=new LinkedList<Map>(); 
		
		List<TbContentCategory> list = contentCategoryService.getListByParentId(id);
		for(TbContentCategory cg : list) {
			Map map=new HashMap();
			map.put("id", cg.getId());
			map.put("parentId", cg.getParentId());
			map.put("text", cg.getName());
			map.put("state", cg.getIsParent()?"closed":"open");
			result.add(map);
		}

		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public TaotaoResult updateNameById(Long id,String name) {
		TaotaoResult result = contentCategoryService.updateNameById(id, name);
		return result;
	}
	
	@RequestMapping("/create")
	@ResponseBody
	public TaotaoResult createContentCategory(Long parentId,String name) {
		TaotaoResult result = contentCategoryService.createContentCategory(parentId, name);
		return result;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public TaotaoResult removeContentCategory(Long parentId,Long id) {
		TaotaoResult result = contentCategoryService.removeContentCategory(parentId, id);
		return result;
	}
	
}
